﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.pro2e.web.util;
using com.pro2e.console.service.log.vo;
using sexhealth.console.service.post;
using sexhealth.console.service.activity.vo;
using System.IO;
using com.pro2e.console.core.util;

public partial class activityAdmin_UC01_Statistics : System.Web.UI.Page
{
    ActivityService activityService = new ActivityService();
    SessionHelper sHelper = new SessionHelper();

    protected void Page_Load(object sender, EventArgs e)
    {
        ((activityAdmin_AdmMasterPage)Master).setPrgName("問卷統計報表");

        if (!IsPostBack)
        {
            
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DateTime startDate = datePicker.DateStart;
        DateTime endDate = ConvertUtil.ToDateTimeMax(datePicker.DateEnd);

        lblTitle.Text = string.Format("期間:{0}年{1}月{2}日起至{3}年{4}月{5}日"
            , startDate.Year, startDate.Month, startDate.Day
            , endDate.Year, endDate.Month, endDate.Day);

        IList<StatisticVO> statisticList = new List<StatisticVO>();
        StatisticVO totalStatisticVO = new StatisticVO();
        totalStatisticVO.Title = "合計";
        totalStatisticVO.CountOption1_1 = 0;
        totalStatisticVO.CountOption1_2 = 0;
        totalStatisticVO.CountOption1_3 = 0;

        for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
        {
            StatisticVO statisticVO = GetStatistic(date);
            statisticList.Add(statisticVO);

            totalStatisticVO.CountOption1_1 += statisticVO.CountOption1_1;
            totalStatisticVO.CountOption1_2 += statisticVO.CountOption1_2;
            totalStatisticVO.CountOption1_3 += statisticVO.CountOption1_3;
        }

        statisticList.Add(totalStatisticVO);
        gvList.DataSource = statisticList;
        gvList.DataBind();
    }

    private StatisticVO GetStatistic(DateTime date)
    {
        DateTime startDate = ConvertUtil.ToDateTimeMin(date);
        DateTime endDate = ConvertUtil.ToDateTimeMax(date);

        int opt1_1 = activityService.CountOptionCheck(AnswerVO.LatestActivityId, true
        , AnswerType.None, "Option1_1", true, startDate, endDate);
        int opt1_2 = activityService.CountOptionCheck(AnswerVO.LatestActivityId, true
            , AnswerType.None, "Option1_2", true, startDate, endDate);
        int opt1_3 = activityService.CountOptionCheck(AnswerVO.LatestActivityId, true
            , AnswerType.None, "Option1_3", true, startDate, endDate);

        StatisticVO statisticVO = new StatisticVO();
        statisticVO.Date = date;
        statisticVO.CountOption1_1 = opt1_1;
        statisticVO.CountOption1_2 = opt1_2;
        statisticVO.CountOption1_3 = opt1_3;

        return statisticVO;
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        lblTitle.Visible = true;
        string attachment = "attachment; filename=Statistics.xls";
        Response.Charset = "big5";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        pnlExport.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        lblTitle.Visible = false;
    }
}